

#ifndef OpenGL_Sweep_Model_h
#define OpenGL_Sweep_Model_h


#include "Model3d.h"
#include "Utils.h"
#include "GL/glut.h"
#include "Defines.h"
#include "Vector3d.h"
#include <iostream>
#include <math.h>
#include <cctype>
using namespace std;


class Sweep: public Model3d {
private:
	Vector3d p_start, p_end;
	Vector3d *v;
	int length_of_vertices;
	double scale;
	double extend;
	double roll;

public:
	Sweep(Vector3d start, Vector3d end, Vector3d *v, int len,double scale=1,double extend=0,double roll=0) {
		p_start = start;
		p_end = end;
		this->v = v;
		length_of_vertices = len;
		this->scale = scale;
		this->extend = extend;
		this->roll = roll;
	}
	void draw() {
		//glutSolidCube(10);
		glPushMatrix();
		{
			//glLoadIdentity();
			//translate
			Vector3d rot_dir = (p_end - p_start).unit();


			Vector3d projectedOnZX = Vector3d(rot_dir.x,0,rot_dir.z).unit();
			Vector3d first_rot_axis = CROSS(-K,projectedOnZX).unit();
			double alpha = acos(DOT(-K,projectedOnZX))*180/PI;

			Vector3d projectedOnYZ = Vector3d(0,rot_dir.y,rot_dir.z).unit();
			Vector3d second_rot_axis = CROSS(-K,projectedOnYZ).unit();
			double beta = acos(DOT(projectedOnZX,rot_dir))*180/PI;

			//if(alpha)glRotatef(alpha,0,first_rot_axis.y,0);
			//if(beta)glRotatef(beta,second_rot_axis.x,0,0);
			//cout << alpha << endl;
			//cout << beta << endl;
			//projectedOnZX.showln();
			//first_rot_axis.showln();
//			projectedOnYZ.showln();
//			second_rot_axis.showln();
			//transform.reset();
			transform.walk(p_start.z);
			transform.strafe(p_start.x);
			transform.fly(p_start.y);
			if(alpha)transform.yaw(first_rot_axis.y*alpha);
			if(beta)transform.pitch(beta*second_rot_axis.x);
			transform.roll(roll);

			transform.update();

			//glutSolidCube(30);

			//draw
			glBegin(GL_QUADS);
			{
				double z = -pointDistance(p_start, p_end);
				for (int i = 0; i < length_of_vertices; i++) {
					Vector3d a = v[i + 1] - v[i];
					Vector3d b = Vector3d(v[i + 1].x, v[i + 1].y, z)
							- Vector3d(v[i].x, v[i].y, 0);
					Vector3d norm = CROSS(a, b).unit();
					glNormal3f(CO(norm));
					glVertex3f(v[i].x * scale, v[i].y * scale, extend);
					glVertex3f(v[i + 1].x * scale, v[i + 1].y * scale, extend);
					glVertex3f(v[i + 1].x * scale, v[i + 1].y * scale, z-extend);
					glVertex3f(v[i].x * scale, v[i].y * scale, z-extend);
				}
			}
			glEnd();
		}
		glPopMatrix();

	}
};




#endif
